package demo.gateway.config;

import demo.common.auth.utils.RsaUtils;
import demo.common.exception.SnowException;
import demo.common.exception.SnowExceptionEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.security.PublicKey;

@Data
@Slf4j
@Component
@ConfigurationProperties(prefix = "test.jwt")
public class JwtProperties implements InitializingBean {

    //读取配置文件，给对象属性赋值
    private String pubKeyPath;

    // 通过RsaUtils工具类获取公钥
    private PublicKey publicKey;

    private UserTokenProperties user = new UserTokenProperties();

    /**
     * 当对象的属性赋值后，要处理的事情
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            // 获取公钥和私钥
            publicKey = RsaUtils.getPublicKey(pubKeyPath);
        } catch (Exception e) {
            log.error("【网关中心】读取公钥失败");
            e.printStackTrace();
            throw new SnowException(500, "服务器内部错误");
        }
    }

    @Data
    public class UserTokenProperties {
        // 存放token的cookie名称
        private String cookieName;
    }
}
